<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
class GoodsListController extends Controller{
	private $goodsListModel;
	//构造方法
	public function __init(){
		//实例化货品列表
		$this->goodsListModel=new \Common\Model\Goodslist;
	}
	//首页方法
	public function index(){
		$gid=Q("get.gid");
		// P($gid);
		//实例化商品属性表
		$goodsAttrModel=new \Common\Model\Gnature;
		$typeAttrModel=new \Common\Model\Typeattr;
		//调取商品属性数据
		$goodsAttrData=$goodsAttrModel->get();
		// p($goodsAttrData);

		//整理商品属性
		foreach($goodsAttrData as $v){
			$newGoodsAttrData[$v['gnid']]=$v['gnvalue'];

		}
		//调取数据
		$attrData=$goodsAttrModel->join('typeattr','typeattr_taid','=','taid')->where("good_gid={$gid} AND class=0")->get();
		$arrData = array();
		foreach ($attrData as $k=> $v) {
			$arrData[$v['taname']][] = $v;
		}
		//分配数据
		View::with('arrData',$arrData);
		// p($arrData);
		//调取货品列表数据
		$goodsListData=$this->goodsListModel->where("good_gid=".Q('get.gid'))->get();
		// p($goodsListData);
		foreach($goodsListData as $k=>$v){
			$arr=explode(',', $v['group_id']);
			$arrs=array();
			foreach($arr as $v){
				$arrs[]=$newGoodsAttrData[$v];
			}
			$goodsListData[$k]['group_id']=$arrs;
		}
		// p($goodsListData);
		// p($arrData);
		//分配数据
		View::with('goodsListData',$goodsListData);
		//引入模板
		View::make();
	}
	//添加方法
	public function add(){
		// p($_POST);die;
		// $_POST['group_id']=implode(',', $_POST['group_id']);
		if(!$this->goodsListModel->store()) View::error($this->goodsListModel->getError());
		View::success('添加成功');
	}
	//删除方法
	public function del(){
	    $glid=Q('get.glid');
		$this->goodsListModel->where("glid=$glid")->delete();
		View::success('删除成功');
	}
	//编辑方法
	public function edit(){
		$gid=Q("get.gid");
		if(IS_POST){
			// $_POST['group_id']=implode(',', $_POST['group_id']);
			if(!$this->goodsListModel->edit()) View::error($this->goodsListModel->getError());
			View::success('修改成功',U('index',array('gid'=>$gid)));
		}
		$glid=Q("get.glid");
		//实例化商品属性表
		$goodsAttrModel=new \Common\Model\Gnature;
		//调取商品属性数据
		$goodsAttrData=$goodsAttrModel->get();

		//整理商品属性
		foreach($goodsAttrData as $v){
			$newGoodsAttrData[$v['gnid']]=$v['gnvalue'];
		}
		//调取数据

		$attrData=$goodsAttrModel->join('typeattr','typeattr_taid','=','taid')->where("good_gid={$gid} AND class=1")->get();
		foreach ($attrData as $k => $v) {
			$newData[$v['taname']][$v["gnid"]]=$v['gnvalue'];
		}
		//分配数据
		// p($attrData);
		View::with('newData',$newData);
		//调取货品列表数据
		$goodsListData=$this->goodsListModel->where("glid=".Q('get.glid'))->find();
		$arr=explode(',', $goodsListData['group_id']);
		foreach($arr as $v){
			$arrs[]=$newGoodsAttrData[$v];
		}
		$goodsListData['group_id']=$arrs;
//		p($newData);
//		p($goodsListData);
		//分配数据
		View::with('goodsListData',$goodsListData);
		//引入模板
		View::make();
	}
	
	
}














 ?>